<?php
/**
 * Created by PhpStorm.
 * User: 冰封~零度
 * Date: 2018/10/11
 * Time: 10:26
 */

namespace app\mobile\controller;

use think\Controller;
use think\Db;
use think\Session;

class Log extends Controller
{
    // 登录页
    public function Login()
    {
        return view('log/login');
    }

    // 处理登录
    public function dologin()
    {
        // 接收数据
        $tel = input('tel');
        $password = input('password');

        // 判断数据
        $data = Db::table('zz_user')
                ->where('zz_pass',$password)
                ->where('zz_tel',$tel)
                ->find();
        if ($data == null){
            $res['status'] = 0;
            return json($res);
        } else {
            // 存Session
            Session::set('puser',$data);
            // 返回
            $res['status'] = 1;
            return json($res);
        }
    }

    // 注册
    public function register()
    {
        return view('log/register');
    }

    // 处理注册
    public function doregister()
    {
        // 接收数据
        $tel = input('tel');
        $password = input('password');
        $username = input('username');
        // 判断是否有数据为空
        if (empty($tel) || empty($password) || empty($username)){
            $res['status'] = 2;
            return json($res);
        }
        // 判断是否有数据重复
        $test = Db::table('zz_user')->where('zz_tel',$tel)->find();
        // 若无数据重复，可注册
        if ($test == null && (strlen($tel) == 11)){
            // 添加数据
            $data = [];
            $data['zz_tel'] = $tel;
            $data['zz_pass'] = $password;
            $data['zz_time'] = date('Y-m-d');
            $data['zz_name'] = $username;
            Db::table('zz_user')->insert($data);
            // 返回数据
            $res['status'] = 1;
            return json($res);
        } else {
            $res['status'] = 0;
            return json($res);
        }

    }

    // 退出
    public function out()
    {
        // 删除Session
        Session::delete('puser');
        // 返回
        $res['status'] = 1;
        return json($res);
    }

    // 找回密码
    public function findpass()
    {
        return view('log/findpass');
    }

    // 移动端找回密码之手机号判断
    public function dophone()
    {
        // 接收数据
        $tel = input('tel');
        if (strlen($tel) != 11 ){
            // 判断位数
            $res['status'] = 1;
            return json($res);
        } else{
            $data = Db::table('zz_user')->where('zz_tel',$tel)->find();
            if (empty($data)){
                // 判断有无此号码
                $res['status'] = 2;
                return json($res);
            } else {
                $res['status'] = 3;
                $res['tel'] = $tel;
                return json($res);
            }
        }
    }

    // 加载找回修改密码页面
    public function dopass($tel)
    {
        return view('log/dopass',[
            'tel' => $tel
        ]);
    }

    // 找回密码之修改密码
    public function donewpass()
    {
        // 接收数据
        $tel = input('tel');
        $newpass = input('newpass');
        $newpassagain = input('newpassagain');
        // 判断两次输入是否一样
        if ($newpass != $newpassagain){
            $res['status'] = 1;
            return json($res);
        } else {
            // 判断新密码是否和旧密码一样
            $pass = Db::table('zz_user')->field('zz_pass')->where('zz_tel',$tel)->find();
            if ($pass['zz_pass'] == $newpassagain){
                $res['status'] = 2;
                return json($res);
            } else {
                // 更新数据
                $data['zz_pass'] = $newpassagain;
                Db::table('zz_user')->where('zz_tel',$tel)->update($data);
                $res['status'] = 3;
                return json($res);
            }
        }
    }


}